# -*- coding: utf-8 -*-
import json,os
from jinja2 import Markup
from flask import flash, url_for, redirect, request, current_app
from flask_admin.base import expose
from flask_admin.model.template import EndpointLinkRowAction, LinkRowAction
from flask_wtf import FlaskForm
from sqlalchemy import func
from wtforms import SelectMultipleField, ValidationError
from wtforms.validators import DataRequired
from flask_admin.actions import action
from flask_admin.helpers import (get_form_data, validate_form_on_submit,get_redirect_target, flash_errors)
from flask_admin.contrib.sqla import form, filters as sqla_filters, tools
from . import MyModelView
from . import AjaxQueryIndexLike
from adminApp.extensions import db, redis_store


class DefaultNicFlowDataView(MyModelView):
    can_edit = False
    can_create = False
    # can_delete = False
    column_filters = ('machine.idc.name', 'machine.idc.mark', 'machine.hostname', 'machine.man_mark',
                      'machine.aqy_device_id', 'ten_bt_sent', 'ten_bt_recv',
                      'flow_datetime',)

    column_list = ('machine.idc.name', 'machine.idc.mark', 'machine.hostname', 'machine.man_mark', 'machine.aqy_device_id', 'bytes_sent', 'bytes_recv', 'errin',
                   'dropin', 'errout', 'dropout', 'ten_bt_sent', 'ten_bt_recv', 'flow_datetime')
    # column_sortable_list = (
    #     'id', 'name', 'category.name', 'author.name', 'serial_state', 'words', 'last_updated', 'created')

    # column_editable_list = ('remark', )

    details_modal = True

    column_labels = {
        'machine.idc.name': '机房名称',
        'machine.idc.mark': '机房分段',
        'machine.hostname': '主机名称',
        'machine.man_mark': '设备标识',
        'machine.aqy_device_id': '设备ID',
        'ten_bt_sent': '10分内发送',
        'ten_bt_recv': '10分内接收',
        'ten_pk_sent': '10分内发包',
        'ten_pk_recv': '10分内收包',
        'flow_datetime': '记录日期',
    }
    column_details_exclude_list = ('id', )
    column_formatters = dict(
        created=lambda v, c, m, p: m.created.strftime("%Y-%m-%d %H:%M:%S"),
        updated=lambda v, c, m, p: m.updated.strftime("%Y-%m-%d %H:%M:%S") if m.updated else '',
        flow_datetime=lambda v, c, m, p: m.flow_datetime.strftime("%Y-%m-%d %H:%M:%S") if m.flow_datetime else ''
    )